﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace Faust.Andrew.MTG.Player
{
    public class DockSettingManager
    {
        private string _path;

        public DockSettingManager(string path)
        {
            _path = path;
        }

        public void WriteSetting(DockSetting setting)
        {
            DockSettings dockSettings = ReadSettings();
            DockSetting currentSetting = dockSettings.Settings.FirstOrDefault(tempSetting => tempSetting.Name == setting.Name);
            if (currentSetting != null)
            {
                dockSettings.Settings.Remove(currentSetting);
            }

            dockSettings.Settings.Add(setting);
            WriteSettings(dockSettings);
        }

        public DockSetting ReadSetting(string name)
        {
            DockSettings dockSettings = ReadSettings();
            return dockSettings.Settings.FirstOrDefault(setting => setting.Name == name);
        }

        public WindowSettings ReadWindowSettings()
        {
            return ReadSettings().WindowSettings;
        }

        public void WriteWindowSettings(WindowSettings settings)
        {
            DockSettings dockSettings = ReadSettings();
            dockSettings.WindowSettings = settings;
            WriteSettings(dockSettings);
        }

        private DockSettings ReadSettings()
        {
            if (!File.Exists(_path)) { return new DockSettings(); }
 
            using (TextReader reader = File.OpenText(_path))
            {
                XmlSerializer xml = new XmlSerializer(typeof(DockSettings));
                return (DockSettings)xml.Deserialize(reader);
            }
        }

        private void WriteSettings(DockSettings settings)
        {
            using (TextWriter writer = File.CreateText(_path))
            {
                XmlSerializer xml = new XmlSerializer(typeof(DockSettings));
                xml.Serialize(writer, settings);
            }
        }        
    }

    public class DockSettings
    {        
        public DockSettings()
        {            
            Settings = new List<DockSetting>();
            WindowSettings = new WindowSettings();
        }

        public WindowSettings WindowSettings { get; set; }
        public List<DockSetting> Settings { get; set; }        
    }

    public class WindowSettings
    {
        public WindowSettings()
        {
            IsWindowMaximized = true;
        }

        public bool IsWindowMaximized { get; set; }
        public double WindowWidth { get; set; }
        public double WindowHeight { get; set; }
    }

    public class DockSetting
    {
        public double Top { get; set; }
        public double Left { get; set; }
        public double Width { get; set; }
        public double Height { get; set; }
        public string Name { get; set; }
        public double Zoom { get; set; }

        public DockSetting()
        {
            Zoom = 1.0;
        }
    }
}
